package values.enlist
{
	import flash.utils.Dictionary;

	public class EnlistTemplateValueList
	{
		
		public static function get isInited():Boolean
		{
			return ( _enlistList != null );
		}
		
		private static var _enlistList:Dictionary;

		public static function get enlistList():Dictionary
		{
			return _enlistList;
		}
		
		public function get enlistList():Dictionary
		{
			return _enlistList;
		}

		[Data(type="mapping" , mapping="avatar",
			   ref="$@classRef",
			   way="one_to_more",
			   object="flash.utils.Dictionary",
	           key="@key")]
		public function set enlistList(value:Dictionary):void
		{
			_enlistList = value;
			
			for each(var etv:EnlistTemplateValue in _enlistList)
			{
				var arr:Array = enlistAvatarTypeDic[etv.avatarTemplate.avatarType.key];
				if(!arr)
				{
					arr = [];
					enlistAvatarTypeDic[etv.avatarTemplate.avatarType.key] = arr;
				}
				
				arr.push(etv);
			}
			
			for each(var arrTemp:Array in enlistAvatarTypeDic)
			{
				arrTemp.sortOn("reputation" , Array.NUMERIC);
			}
		}
		
		public static function getEnlistByKey(value:String):EnlistTemplateValue
		{
			return _enlistList[value] as EnlistTemplateValue;
		}
        
        public static function getEnlistByAvatarKey(value:String):EnlistTemplateValue
        {
            for each (var template:EnlistTemplateValue in _enlistList)
            {
                if (template.avatarKey == value) return template;
            }
            return null;
        }
		
		private static var enlistAvatarTypeDic:Dictionary = new Dictionary(true);
		
		public static function getEnlistByType(value:String):Array
		{
			return enlistAvatarTypeDic[value] as Array;
		}
	}
}